﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using TED.Model;

namespace TED.ViewModels
{
    public class FavouriteVideoModel : INotifyPropertyChanged
    {
        private const string FILE_NAME = "favourite.xml";

        #region Constructor
        public FavouriteVideoModel()
        {
            FavouriteVideoList = new ObservableCollection<TEDVideo>();
            GetListOfFavouriteVideos();
        }
        #endregion

        #region GetListOfFavouriteVideos
        public void GetListOfFavouriteVideos()
        {
            //Read from the Isolated Storage
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(FILE_NAME, FileMode.Open))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<TEDVideo>));
                        FavouriteVideoList = (ObservableCollection<TEDVideo>)serializer.Deserialize(stream);
                    }
                }
            }
            catch
            {
                //add some code here
            }
        }
        #endregion

        #region AddVideoToFavourite
        public void AddVideoToFavourite(TEDVideo t)
        {
            this.GetListOfFavouriteVideos();
            FavouriteVideoList.Add(t);
            SaveFavouriteToStorage();
        }

        private void SaveFavouriteToStorage()
        {
            // Write to the Isolated Storage
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(FILE_NAME, FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<TEDVideo>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, FavouriteVideoList);
                    }
                }
            }
        }
        #endregion

        #region RemoveVideoFromFavourite
        public void RemoveVideoFromFavourite(string viewUrl)
        {
            GetListOfFavouriteVideos();
            try
            {
                for (int i = 0; i < FavouriteVideoList.Count; ++i)
                {
                    if ((FavouriteVideoList[i].ViewUrl.Equals(viewUrl)) == true)
                    {
                        FavouriteVideoList.RemoveAt(i);
                        --i;
                    }
                }
       
                //Update playlist
                SaveFavouriteToStorage();    
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        #endregion

        // Collections
        #region FavouriteVideoList
        private ObservableCollection<TEDVideo> _FavouriteVideoList;
        public ObservableCollection<TEDVideo> FavouriteVideoList
        {
            get { return _FavouriteVideoList; }
            private set
            {
                if (FavouriteVideoList == value)
                {
                    return;
                }
                _FavouriteVideoList = value;
                this.NotifyPropertyChanged("FavouriteVideoList");
            }
        }
        
        #endregion

        // Utility

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
